<template>
    <view>
        <button size="mini" @click="wxLogin">微信登录</button>
    </view>


    <!-- 测试一下云函数的增删改查，在熟练之后去掉 -->
    <view v-for="(item, index) in students" :key="item._id">
        <view>{{item.name}}</view>
        <button size="mini" type="warn" @click="remove(item._id)">删除</button>
        <button size="mini" type="warn" @click="edit(item._id)">编辑</button>
    </view>
</template>

<script setup>
    import {
        ref
    } from 'vue';
    import {
        onLoad
    } from "@dcloudio/uni-app"

    let students = ref();

    // TODO: 一开始进来的时候，判断登录态，拉取单词库列表
    onLoad(() => {
        // 查询数据
        uniCloud.callFunction({
            name: "student-list",
        }).then(res => {
            students.value = res.result.data;
        })

        // uniCloud.callFunction({
        //     name: "cloudFun2",
        // }).then(res => {
        //     console.log(res)
        // })
    })

    function wxLogin() {
        uni.getUserInfo({
            provider: 'weixin',
            desc: "登录的数据",
            success(user) {
                user.authResult
                uni.login({
                    provider: 'weixin',
                    onlyAuthorize: true,
                    success(lres) {
                        console.log(lres, "请求微信登录返回的数据")

                        if (lres.code) {
                            uni.request({
                                url: "https://api.weixin.qq.com/sns/jscode2session",
                                method: 'GET',
                                data: {
                                    appid: 'wxf6ab670df0a89e37',
                                    secret: '51529562462349b6aebfc6c9c3950b31',
                                    js_code: lres.code,
                                    grant_type: 'authorization_code'
                                },
                                success: (res) => {
                                    console.log("获取 session res", res)
                                },
                                fail: (res) => {
                                    console.log("获取 session res 失败", res)
                                }
                            })
                        }
                    }
                })
            }
        })
    }

    function remove(id) {
        // 删除逻辑
        uniCloud.callFunction({
            name: "student-remove",
            data: {
                id,
            }
        }).then(res => {
            wx.showToast({
                title: "删除成功"
            })

            // 查询数据
            uniCloud.callFunction({
                name: "student-list",
            }).then(res => {
                students.value = res.result.data;
            })
        })
    }

    function edit(id) {
        uniCloud.callFunction({
            name: "student-edit",
            data: {
                id,
                name: "瓜皮钊",
                age: 18,
                score: 100
            }
        }).then(res => {
            wx.showToast({
                title: "编辑成功"
            })

            // 查询数据
            uniCloud.callFunction({
                name: "student-list",
            }).then(res => {
                students.value = res.result.data;
            })
        })
    }
</script>

<style>

</style>